package com.activeMq.affair;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class Producer {
    public static void main(String[] args) throws JMSException {
        ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
        Connection connection = factory.createConnection();
        connection.start();
        //true 开启事务
        Session session = connection.createSession(true, Session.CLIENT_ACKNOWLEDGE);
        Queue queue = session.createQueue("tx_queue_affair");
        MessageProducer producer = session.createProducer(queue);
        try {
            for (int i = 0; i < 10; i++) {
                TextMessage message = session.createTextMessage("你好吗" + i);
                if (i==3){
                throw new  Exception("cx");
                }

                producer.send(message);
            }
            session.commit();
        } catch (Exception e) {
            session.rollback();
        }


        producer.close();
        session.close();
        connection.close();
    }
}
